package cn.sinjet.adapter;

import java.util.List;

import com.mgroup.wazehelper.R;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import cn.sinjet.phone.Contact;
/**
 * @author Kevin
 */
public class ContactsAdapter extends BaseAdapter {
	public static final int TYPE_CALLLOG = 0;
	public static final int TYPE_CONTACT = 1;
	private Context mContext;
	private List<Contact> mContactList = null;
	private LayoutInflater inflater;
	int selectedPos = -1;
	public ContactsAdapter(List<Contact> list, Context context) {
		mContactList = list;
		mContext = context;
		inflater = LayoutInflater.from(mContext);	
	}

	public void setContactListItem(List<Contact> list){
		mContactList = list;
	}
	
	@Override
	public int getCount() {
		if(mContactList != null)
		return mContactList.size();
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if(mContactList == null || position > mContactList.size())
			return null;
		return mContactList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if(convertView == null){
			convertView = inflater.inflate(R.layout.phone_contact_item, null);
			holder = new ViewHolder();
			holder.name = (TextView)convertView.findViewById(R.id.result_name);
			holder.number = (TextView) convertView.findViewById(R.id.result_address);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		if(position == selectedPos){
			Log.d("contact", "position == selectedPos:"+position);
		    convertView.setBackgroundColor(mContext.getResources().getColor(R.color.item_pressed));
		}
		else
			convertView.setBackgroundResource(R.drawable.listitem_selector);
		
		String name = mContactList.get(position).getName();
		String number = mContactList.get(position).getNumber();
		if(!name.equals(number))
		    holder.name.setText(name);
		else holder.name.setText("未知");
		
		holder.number.setText(number);
		return convertView;
	}
	
	public static class ViewHolder {
		TextView  name;
		TextView  number;
	}

	public void setSelectedPos(int pos) {
		// TODO Auto-generated method stub
		Log.d("contact", "setSelectedPos:"+pos);
		selectedPos = pos;
		notifyDataSetInvalidated();
	}
	
}
